package com.compunit.composite.core.cx;

import com.compunit.composite.core.interfaces.cx.ICxUnit;

/**
 * <code>CxUnit</code> defines the implementation of
 * the data structure that represents a component unit
 * in activation order.
 *
 * @author Yi Wang (Neakor)
 * @version Creation date: 11-21-2009 18:40 EST
 * @version Modified date: 12-05-2009 14:09 EST
 */
public class CxUnit implements ICxUnit {
	/**
	 * The <code>String</code> fully qualified component
	 * primary class name.
	 */
	private String classname;
	/**
	 * The <code>String</code> component resource file name.
	 */
	private String resourcename;
	
	/**
	 * Constructor of <code>CxUnit</code>.
	 */
	public CxUnit() {}
	
	/**
	 * Constructor of <code>CxUnit</code>.
	 * @param classname The <code>String</code> fully
	 * qualified component primary class name.
	 */
	public CxUnit(final String classname) {
		this.setClassname(classname);
	}
	
	/**
	 * Constructor of <code>CxUnit</code>.
	 * @param classname The <code>String</code> fully
	 * qualified component primary class name.
	 * @param resourcename The <code>String</code>
	 * component resource file name.
	 */
	public CxUnit(final String classname, final String resourcename) {
		this.setClassname(classname);
		this.setResourcename(resourcename);
	}

	@Override
	public void setClassname(final String classname) {
		if(classname == null) return;
		this.classname = classname;
	}

	@Override
	public void setResourcename(final String resourcename) {
		this.resourcename = resourcename;
	}

	@Override
	public String getClassname() {
		return this.classname;
	}

	@Override
	public String getResourcename() {
		return this.resourcename;
	}

	@Override
	public boolean equals(final Object o) {
		if(o instanceof CxUnit) {
			final CxUnit given = (CxUnit)o;
			boolean cb = false;
			if(this.classname == null && given.classname == null) cb = true;
			else if(this.classname != null && given.classname != null) cb = this.classname.endsWith(given.classname);
			if(!cb) return false;
			boolean rb = false;
			if(this.resourcename == null && given.resourcename == null) rb = true;
			else if(this.resourcename != null && given.resourcename != null) rb = this.resourcename.endsWith(given.resourcename);
			if(!rb) return false;
			return true;
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		final int chash = this.classname==null ? 0 : this.classname.hashCode();
		final int rhash = this.resourcename==null ? 0 : this.resourcename.hashCode();
		return chash+rhash;
	}
}
